/*
 *  Copyright (C) 2010 Fabien Renaud
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.boc.botv.service.impl;

import com.boc.botv.common.PaymentMethodStatus;
import com.boc.botv.dao.PaymentMethodDao;
import com.boc.botv.model.PaymentMethod;
import com.boc.botv.service.PaymentMethodManager;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Fabien Renaud
 */
@Service("paymentMethodManager")
//@Transactional(readOnly = true)
public class PaymentMethodManagerImpl implements PaymentMethodManager {

  private final Log logger = LogFactory.getLog(PaymentMethodManagerImpl.class);
  private PaymentMethodDao paymentMethodDao;

  @Autowired
  public void setPaymentMethodDao(PaymentMethodDao paymentMethodDao) {
    this.paymentMethodDao = paymentMethodDao;
  }

  public List<PaymentMethod> getPaymentMethods() {
    List<PaymentMethod> paymentMethods = paymentMethodDao.getPaymentMethods();
    logger.info("[PaymentMethod] Length of the list : " + paymentMethods.size());

    return paymentMethods;
  }

  public boolean isValid(PaymentMethod paymentMethod) {
    logger.info("[PaymentMethod]Check status of payment method  #`" + paymentMethod + "`");
    int status = paymentMethod.getStatus();

    return status == 2;
  }

  public PaymentMethod getPaymentMethod(int paymentId) {
    logger.info("[PaymentMethod] Getting Status");
    PaymentMethod pm = paymentMethodDao.getPaymentMethod(paymentId);

    return pm;
  }

  //@Transactional(readOnly = false)
  public void createPaymentMethod(PaymentMethod paymentMethod) {
    logger.info("[PaymentMethod] Creation of the payment method : `" + paymentMethod.getId());
    paymentMethodDao.savePaymentMethod(paymentMethod);

  }

  //@Transactional(readOnly = false)
  public boolean disablePaymentMethod(int paymentId) {
    logger.info("[PaymentMethod] Disable payment method of #" + paymentId);
    PaymentMethod pm = paymentMethodDao.getPaymentMethod(paymentId);
    pm.setStatus(PaymentMethodStatus.disabled);

    return true;
  }

  //@Transactional(readOnly = false)
  public boolean enablePaymentMethod(int paymentId) {
    logger.info("[PaymentMethod] Enable payment method of #" + paymentId);
    PaymentMethod pm = paymentMethodDao.getPaymentMethod(paymentId);
    pm.setStatus(PaymentMethodStatus.enabled);

    return true;
  }

  //@Transactional(readOnly = false)
  public boolean updatePaymentMethod(PaymentMethod pm) {
    logger.info("[PaymentMethod] Updating PaymentMethod");
    if (pm == null) {
      return false;
    }
    paymentMethodDao.updatePaymentMethod(pm);

    return true;
  }

  //@Transactional(readOnly = false)
  public int nextStepPaymentMethod(int pmId) {
    logger.info("[PaymentMethod] Updating payment method status #" + pmId);
    if (pmId < 0) {
      return -1;
    }
    PaymentMethod pm = paymentMethodDao.getPaymentMethod(pmId);
    if (pm == null) {

      return -1;
    }
    int status = pm.getStatus();
    if (status == PaymentMethodStatus.disabled) {
      status = PaymentMethodStatus.checking;
    } else {
      status = PaymentMethodStatus.enabled;
    }
    pm.setStatus(status);

    return status;
  }

  //@Transactional(readOnly = false)
  public boolean checkPaymentMethod(int paymentId) {
    logger.info("[PaymentMethod] Starting verification for payment method of #" + paymentId);
    PaymentMethod pm = paymentMethodDao.getPaymentMethod(paymentId);
    pm.setStatus(PaymentMethodStatus.checking);

    return true;
  }

  //@Transactional(readOnly = false)
  public boolean refusePaymentMethod(int paymentId) {
    logger.info("[PaymentMethod] Refusing payment method #" + paymentId);
    PaymentMethod pm = paymentMethodDao.getPaymentMethod(paymentId);
    pm.setStatus(PaymentMethodStatus.refused);

    return true;
  }
}
